package org.example.runningforum.Controller;



import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.runningforum.modle.Result;
import org.example.runningforum.modle.User;
import org.example.runningforum.server.UserService;
import org.example.runningforum.utils.JwtUtils;
import org.example.runningforum.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.Map;

import static org.example.runningforum.utils.JwtUtils.getUserInfoID;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUserCount")
    public Integer getUserCount(){
        return userService.selectUserCount();
    }

    @RequestMapping("/login")
    public Result login(User userInfo){
//        参数判断
        if(userInfo==null|| !StringUtils.hasLength(userInfo.getUsername() )|| !StringUtils.hasLength(userInfo.getPassword())){
            return Result.Fail("请合法输入");
        }

        String username = userInfo.getUsername();
        User userInfo1 = userService.selectUserByUsername(username);
        log.info("接收到的密码为：{}",userInfo1);


        if(userInfo1.getPassword()==null|| !SecurityUtil.verify(userInfo.getPassword(),userInfo1.getPassword())){
            return Result.Fail("账号或密码错误");
        }

//        账号密码正确，生成token并返回
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",userInfo1.getUserId());
        claims.put("userName",userInfo.getUsername());
        String token = JwtUtils.genToken(claims);
        log.info("设置客户端token，{}",token);
        return Result.success(token);
    }

    /*
     * 根据token获取当前用户信息
     * */
    @RequestMapping("/getUserInfo")
    public  User getUserInfoById(HttpServletRequest request) {
        String token = request.getHeader("user_token_key");
        Integer infoID = null;
        if (StringUtils.hasLength(token)) {
            //获取用户id
            infoID = getUserInfoID(token);
        }
        User userInfo = userService.selectUserById(infoID);
        log.info("从token中获取的用户信息，{}",userInfo);
        return userInfo;
    }

    /*
     * 注册用户
     * */
    @RequestMapping("/enroll")
    public Result enroll(User userInfo){
//        参数判断
        if(userInfo==null|| !StringUtils.hasLength(userInfo.getUsername()) || !StringUtils.hasLength(userInfo.getPassword())){
            return Result.Fail("请合法输入");
        }

        String enroll = userService.enroll(userInfo);
        if("注册成功".equals(enroll)){
            return Result.success("注册成功");
        }else {
            return Result.Fail("注册失败");
        }
    }

    @RequestMapping("/updateUser")
    public String updateUser(User user,HttpServletRequest request){

        String token = request.getHeader("user_token_key");
        Integer infoID = null;
        if (StringUtils.hasLength(token)) {
            //获取用户id
            infoID = getUserInfoID(token);
        }
        user.setUserId(Long.valueOf(infoID));

        Integer n = userService.updateUser(user);
        if(n==1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }



}
